<?php
//share zsn 20170607 16:20
class SharesquareModel extends RelationModel{
    protected $tableName = "";//重新定义表名

    public function getList($queryType,$inputs){//获取自驾游列表
//        dump($inputs);
        $p=$inputs["page"]?$inputs["page"]:1;
        $n=$inputs["rows"]?$inputs["rows"]:10;
        $limit = "limit ".($p-1)*$n.",".$n;
        $sort="";
        if($inputs["sort"]){
            $sort = "order by `".$inputs["sort"]."` ".$inputs["order"];
        }
        $datetime = $inputs["searchTime"];
        $searchStartTime = $inputs["searchStartTime"]?$inputs["searchStartTime"]:'';
        $searchEndTime = $inputs["searchEndTime"]?$inputs["searchEndTime"]:'';
//echo $datetime;
        $where = "";
//            $where .=" AND A.inputtime > FROM_UNIXTIME(". (time()-3600*24*3) .") ";
        if($datetime=="0")//最近3天FROM_UNIXTIME
            $where .=" AND smbi_time > '". date("Y-m-d",(time()-3600*24*3)) ."' ";
        elseif($datetime=="1")//最近7天
            $where .=" AND smbi_time > '". date("Y-m-d",(time()-3600*24*7)) ."' ";
        elseif($datetime == "2")//最近15
            $where .=" AND smbi_time > '". date("Y-m-d",(time()-3600*24*15)) ."' ";
        elseif($datetime == "3")//最近30
            $where .=" AND smbi_time > '". date("Y-m-d",(time()-3600*24*30)) ."' ";
        elseif($datetime == "4")//最近60
            $where .=" AND smbi_time > '". date("Y-m-d",(time()-3600*24*60)) ."' ";
        elseif($datetime == "5")//最近90
            $where .=" AND smbi_time > '". date("Y-m-d",(time()-3600*24*90)) ."' ";
        elseif($datetime == "time"){
            if($searchStartTime=='searchEndTime')
                $searchStartTime='';
            if(!$searchStartTime == '' )//如果有起始时间
            {
                $where .=" AND smbi_time > '".($searchStartTime)."' ";
            }else{
                $where .=" AND smbi_time > '".(date("Y-m-d",time()-3600*24*3)) ."' ";
            }
            if(!$searchEndTime == '')//如果有截至时间
            {
                $where .= " AND smbi_time < '".($searchEndTime)."' ";
            }
        }
        //=============
        $model = M();
//        $where = "";

        if ($inputs['op'] == 'getShareSquare') {
            if(trim($inputs['searchValue']) != '创建者ID/创建者用户名'){
                $where .= " and ( username like '%".trim($inputs['searchValue'])."%'  or    C.userid like '%".trim($inputs['searchValue'])."%' ) ";
            }
            if($queryType == 'count'){
                $sql = "SELECT COUNT(1) count FROM (select count(1) as count from `square_main_body_interaction`  A
                          LEFT JOIN auto_driving_tour C ON smbi_smbid=C.id
                          LEFT JOIN auto_user B ON C.userid=B.id
                       where 1=1   AND dflag=0 AND smbi_type=3 AND smbi_ctype =(3) ".$where." GROUP BY C.id ) tm  ;";
            }
            if($queryType == 'list'){
                $fields = " count(1) sn, smbi_smbid id ,C.userid,MIN(smbi_time) start_square_time,MAX(smbi_time) end_square_time,smbi_userid,  smbi_smbid,smbi_ctype,   smbi_time ,username,  title,intro,  banner,  banner_thum";
                $sql = "select ".$fields." from square_main_body_interaction A
                          LEFT JOIN auto_driving_tour C ON smbi_smbid=C.id
                          LEFT JOIN auto_user B ON C.userid=B.id
                         where 1=1  AND dflag=0  AND smbi_type=3 AND smbi_ctype =3 ".$where."   GROUP BY smbi_smbid   ".$sort." ".$limit.";";
            }
//            echo $sql;
        }
        if ($inputs['op'] == 'getShareImage') {
            if(trim($inputs['searchValue']) != '创建者ID/创建者用户名'){
                $where .= " and ( username like '%".trim($inputs['searchValue'])."%'  or    smb_create_userid like '%".trim($inputs['searchValue'])."%' ) ";
            }
            if($queryType == 'count'){
                $sql = "select count(1) count from (select count(1) as count from `square_main_body_interaction`  A
                          LEFT JOIN square_main_body C ON smbi_smbid=C.smb_id
                          LEFT JOIN auto_user B ON smb_create_userid=B.id
                       where 1=1  and smb_del_status!=1   AND smbi_type=3 AND smbi_ctype =2   ".$where."  GROUP BY smb_id  ) tmp ;";
            }
            if($queryType == 'list'){
                $fields = "smb_create_userid,
      COUNT(1) sn,     MIN(smbi_time) start_square_time,MAX(smbi_time) end_square_time,
       C.smb_id, smbi_userid,  smbi_smbid, smbi_ctype, smbi_time ,username,  smb_describe ";
//                GROUP_CONCAT(smbc_url1) smbc_url1,GROUP_CONCAT(smbc_url2) smbc_url2

//                LEFT JOIN square_main_body_content D ON D.smbc_smbid=smbi_smbid
                $sql = "select ".$fields." from square_main_body_interaction A
                          LEFT JOIN square_main_body C ON smbi_smbid=C.smb_id
                          LEFT JOIN auto_user B ON smb_create_userid=B.id
                         where 1=1  and smb_del_status!=1 AND smbi_type=3 AND smbi_ctype =2  ".$where."    GROUP BY smb_id    ".$sort." ".$limit.";";
            }
//            echo $sql;
        }
        if ($inputs['op'] == 'getShareVideo') {
            if(trim($inputs['searchValue']) != '创建者ID/创建者用户名'){
                $where .= " and ( username like '%".trim($inputs['searchValue'])."%'  or    smb_create_userid like '%".trim($inputs['searchValue'])."%' ) ";
            }
            if($queryType == 'count'){
                $sql = "select count(1) as count from (select count(1) as count from `square_main_body_interaction`  A
                          LEFT JOIN auto_user B ON smbi_userid=B.id
                          LEFT JOIN square_main_body C ON smbi_smbid=C.smb_id
                          LEFT JOIN square_main_body_content D ON D.smbc_smbid=smbi_smbid
                       where 1=1 and smb_del_status!=1 AND smbc_del_status!=1   AND smbi_type=3 AND( smbi_userid != '0' )  AND smbi_ctype =1 ".$where."GROUP BY smb_id ) tm;";
            }
            if($queryType == 'list'){
                $fields = "     COUNT(1) sn,     MIN(smbi_time) start_square_time,MAX(smbi_time) end_square_time,
                 C.smb_id,smb_create_userid, smbi_userid,  smbi_smbid,smbi_ctype,  smbi_time ,username,    smb_describe,smbc_url1,smbc_url2";
                $sql = "select ".$fields." from square_main_body_interaction A
                          LEFT JOIN square_main_body C ON smbi_smbid=C.smb_id
                          LEFT JOIN auto_user B ON smb_create_userid=B.id
                          LEFT JOIN square_main_body_content D ON D.smbc_smbid=smbi_smbid
                         where 1=1  and smb_del_status!=1 AND smbc_del_status!=1  AND smbi_type=3 AND( smbi_userid != '0' )  AND smbi_ctype =1 ".$where." GROUP BY smb_id   ".$sort." ".$limit.";";
            }
//            echo $sql;
        }
        if ($inputs['op'] == 'getShareTourVideo') {
            if(trim($inputs['searchValue']) != '创建者ID/创建者用户名'){
                $where .= " and ( username like '%".trim($inputs['searchValue'])."%'  or    D.userid like '%".trim($inputs['searchValue'])."%' ) ";
            }
            if($queryType == 'count'){
                $sql = "select count(1) count from (select count(1) as count from `square_main_body_interaction`  A
                          LEFT JOIN auto_driving_tour D ON D.id=smbi_pid
                          LEFT JOIN auto_tour_event C ON smbi_smbid=C.id
                          LEFT JOIN auto_user B ON smbi_userid=B.id
                         WHERE 1=1   AND dflag=0 AND smbi_type=3 AND( smbi_ctype >30 )".$where."  GROUP BY smbi_smbid ) tm ;";
            }
            if($queryType == 'list'){
                $fields = "  title smb_describe,   RIGHT(smbi_ctype,1)  c ,D.userid,smbi_pid, COUNT(1) sn,   C.box, smbi_ctype,   smbi_smbid,smbi_ctype ,smbi_userid,MIN(smbi_time) start_square_time,MAX(smbi_time) end_square_time,smbi_time
                ,username";
                $sql = "select ".$fields." from square_main_body_interaction A
                          LEFT JOIN auto_driving_tour D ON D.id=smbi_pid
                          LEFT JOIN auto_user B ON D.userid=B.id
                          LEFT JOIN auto_tour_event C ON smbi_smbid=C.id
                         where 1=1  AND dflag=0  AND smbi_type=3  and D.userid !=''  AND smbi_ctype >30 ".$where."  group by smbi_smbid  ".$sort." ".$limit.";";
//                          LEFT JOIN square_main_body_content D ON D.smbc_smbid=smbi_smbid
            }
//            echo $sql;
        }
        if ($inputs['op'] == 'getShareTourImages') {
            if(trim($inputs['searchValue']) != '创建者ID/创建者用户名'){
                $where .= " and ( username like '%".trim($inputs['searchValue'])."%'  or    smb_create_userid like '%".trim($inputs['searchValue'])."%' ) ";
            }
            if($queryType == 'count'){
                $sql = "select count(1) count from (select count(1) as count from `square_main_body_interaction`  A
                          LEFT JOIN  square_main_body C ON A.smbi_smbid=C.smb_id
                          LEFT JOIN auto_user B ON smb_create_userid=B.id
                         WHERE 1=1  and smb_del_status!=1 AND smbi_type=3 AND  smbi_ctype=32 AND smb_type=32 ".$where."  GROUP BY smbi_smbid ) tm ;";
            }
            if($queryType == 'list'){
                $fields = "    COUNT(1) sn,  smb_id,  smb_describe,    smbi_type, smb_create_userid, smbi_userid,   smbi_smbid, MIN(smbi_time) start_square_time,MAX(smbi_time) end_square_time,  smbi_time,  smbi_ctype,smb_type,username";
                $sql = "select ".$fields." from square_main_body_interaction A
                          LEFT JOIN  square_main_body C ON A.smbi_smbid=C.smb_id
                          LEFT JOIN auto_user B ON smb_create_userid=B.id
                         where 1=1  and smb_del_status!=1 AND smbi_type=3  AND  smbi_ctype=32 AND smb_type=32 ".$where."  GROUP BY smbi_smbid  ".$sort." ".$limit.";";
//                          LEFT JOIN square_main_body_content D ON D.smbc_smbid=smbi_smbid
            }
//            echo $sql;
        }
//            echo $sql;
        $results = $model->query($sql);
        return $results;
    }
    public function getListDetails($queryType,$inputs){//获取自驾游列表
//        dump($inputs);
        $p=$inputs["page"]?$inputs["page"]:1;
        $n=$inputs["rows"]?$inputs["rows"]:10;
        $limit = "limit ".($p-1)*$n.",".$n;
        $sort="";
        if($inputs["sort"]){
            $sort = "order by `".$inputs["sort"]."` ".$inputs["order"];
        }
        $datetime = $inputs["datetime"];
        $searchStartTime = $inputs["searchStartTime"]?$inputs["searchStartTime"]:'';
        $searchEndTime = $inputs["searchEndTime"]?$inputs["searchEndTime"]:'';
//echo $datetime;
        $where = "";
        //=============
        $model = M();

        if ($inputs['op'] == 'getShareSquareDetails') {
            $where .= " AND smbi_ctype= " . $inputs['ctype'] . "  and  smbi_smbid= " . $inputs['id'] . " ";
            if ($queryType == 'count') {
                $sql = "select COUNT(1) count  from square_main_body_interaction A
                          LEFT JOIN auto_user B ON smbi_userid=B.id
                         where 1=1 AND smbi_type=3   " . $where . " ;";
            }
            if ($queryType == 'list') {
                $fields = "   smbi_id,  smbi_type,  smbi_userid,  smbi_smbid,  smbi_time,  smbi_ctype,  smbi_pid ,username
                ";
                $sql = "select " . $fields . " from square_main_body_interaction A
                          LEFT JOIN auto_user B ON smbi_userid=B.id
                         where 1=1 AND smbi_type=3   " . $where . "   " . $sort . " " . $limit . ";";
            }
        }
//        echo $sql;
        $results = $model->query($sql);
        return $results;
    }
    public function getListImages($id){//获取image列表
        $model = M();
        $sql = "select   smbc_smbid, GROUP_CONCAT(smbc_url1) smbc_url1,GROUP_CONCAT(smbc_url2) smbc_url2  from square_main_body_content    where 1=1  AND smbc_del_status!=1 AND smbc_smbid=".$id."  ;";
//            echo $sql;
        $results = $model->query($sql);
        return $results;
    }

}